package com.nxu.admin.config;

import com.nxu.admin.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Author wen
 * Date 2021-11-24
 */

/**
 * 1.编写一个拦截器，实现HandlerInterceptor接口
 * 2.拦截器注册到容器中（实现WebMvcConfigurer的addInterceptors）
 * 3.指定拦截规则【如果拦截所有，静态资源也会被拦截】
 *
 * @EnableWebMvc 全面接管,慎用
 *        静态资源，视图解析器，欢迎页。。全部失效
 */
//@EnableWebMvc
@Configuration
public class AdminWebConfig implements WebMvcConfigurer {

    //定义静态资源
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //访问aa路径下的所有请求，都去/static/ 下面进行匹配
        registry.addResourceHandler("/aa/**")
                .addResourceLocations("classpath:/static/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**")//包括静态资源所以请求都被拦截
                .excludePathPatterns("/","/login","/css/**","/fonts/**","/images/**",
                        "/js/**","/aa/**");//放行的请求
    }
}
